﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConcreteMappersAndObjects.DomainObjects;
using DataMappers;
using System.Data;

namespace ConcreteMappersAndObjects.Mappers
{
    public class RegimesDataMapper : AbstractDataMapper<int, Regime>
    {
        private IEntityMapper<int, SClass> _classesMapper;
        public void SetClassesMapper(IEntityMapper<int, SClass> mapper){ _classesMapper = mapper; }
        protected override int GetKeyFrom(System.Data.SqlClient.SqlDataReader sdr)
        {
            return sdr.GetInt32(0);
        }

        protected override string DoGetAllStatement()
        {
            return "select ID, Description from Regime";
        }

        protected override string DoGetFindStatement()
        {
            return DoGetAllStatement() + " where ID = @ID";
        }

        protected override void BindFindStatement(System.Data.SqlClient.SqlCommand cmd, int key)
        {
            cmd.Parameters.AddWithValue("@ID", key);
        }

        private class RegimeFilter : IFilter
        {
            private int _key;
            public RegimeFilter(int key) { _key = key; }
            #region IFilter Members

            public string GetWhereClause()
            {
                return " where RegimeID = @RegimeID";
            }

            public void BindValues(System.Data.SqlClient.SqlCommand cmd)
            {
                cmd.Parameters.AddWithValue("RegimeID", _key);
            }

            #endregion
        }

        protected override Regime DoLoad(System.Data.SqlClient.SqlDataReader sdr)
        {
            int key = GetKeyFrom(sdr);
            LazySet<int, SClass> classes = new LazySet<int,SClass>(_classesMapper, new RegimeFilter(key));
            return new Regime(key, sdr.GetString(1), classes);
        }

        protected override string GetUpdateCommand()
        {
            return "update Regime set Description = @Description where ID = @ID";
        }

        protected override void DoBindUpdate(System.Data.SqlClient.SqlCommand cmd, Regime t)
        {
            cmd.Parameters.AddWithValue("@Description", t.Description);
            cmd.Parameters.AddWithValue("@ID", t.ID);
        }

        protected override string GetInsertCommand()
        {
            return "RegimeInsert";
        }

        protected override void DoBindInsert(System.Data.SqlClient.SqlCommand cmd, Regime t)
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@Description", SqlDbType.NVarChar, 20).Value = t.Description;
        }

        protected override string GetRemoveCommand()
        {
            return "delete from Regime where ID = @ID";
        }

        protected override void DoBindRemove(System.Data.SqlClient.SqlCommand cmd, Regime t)
        {
            cmd.Parameters.Add("@ID", SqlDbType.Int).Value = t.ID;
        }

        protected override void FinishUpdateObject(Regime t)
        {
            //throw new NotImplementedException();
        }

        protected override void FinishNewObject(Regime t, object id)
        {
            t.ID = Convert.ToInt32(id);
        }

        protected override void FinishDeleteObject(Regime t)
        {
            
        }
    }
}
